Ext.define('KMA.view.grids.PlatosArmados',{
   extend: 'Ext.grid.Panel',
   alias: 'widget.gridplatosarmados',

   initComponent: function(){
       
       this.store = Ext.create('KMA.store.grids.PlatosArmados',{});
       
       var groupingFeature = Ext.create('Ext.grid.feature.Grouping',{
                   groupHeaderTpl: 'Plato: {name} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})'
       });
       
       this.features = groupingFeature;
       
       this.dockedItems = [
           {
               xtype: 'toolbar',
               dock: 'top',
               items: [
                   {xtype: 'button', action: 'txtreport', text: 'Generar reporte impreso', iconCls: 'ico-file'},
               ]
            }
       ];
       
       this.columns = [
            {text: 'Grado', dataIndex: 'grado', sortable: true, flex: 1 },
            {text: 'Plato', dataIndex: 'plato', sortable: true, flex: 1 },
            {text: 'Cantidad', dataIndex: 'cantidad', sortable: false, flex: 1 },
            // Campos ocultos que almacenan los datos de la consulta
            {text:'menudiario', dataIndex: 'menudiario', sortable: false, hidden: true }
            
       ];
       
       this.width = '100%';
       this.height = 450;
       
       this.callParent(arguments);
   },
   isSelectedRow: function(grid){
       var row = this.getSelectionModel().getSelection();
        if(row.length == 0){
            return false;
        }else{
            return true;
        }
   },
   showAlumnos: function(grid){
       if(this.isSelectedRow(grid)){
           this.createStoreAlumnosPlatoArmado();
       }else{
           this.showNotSelectedRow();
       }
   },
   notSelectedRow: function(){
       Ext.create('KMA.view.environment.Error',{
            titulo: "Ver alumnos del plato",
            txt: "No se ha seleccionado ningún plato."
        }).showDialog();
   },
   createStoreAlumnosPlatoArmado: function(){
       var me = this;
       Ext.Ajax.request({
           method: 'POST',
           url: 'src/Drivers/AlumnosPlatoArmado/',
           params: {
               transCode: me.getTransCode(),
               grado: me.getGrado(),
               menudiario: me.getMenuDiario(),
               plato: me.getPlato()
           },
           success: function(response){
                me.successHandler(response);
           }
       });
   },
   getTransCode: function(){
       var form = this.up().up().getComponent('formGridPlatosArmados').down('filtroselecciones');
       return form.getChildByElement('transCode').getValue();
   },
   getGrado: function(){
       var row = this.getSelectionModel().getSelection();
       return row[0].get('grado');
   },
   getMenuDiario: function(){
       var row = this.getSelectionModel().getSelection();
       return row[0].get('menudiario');
   },
   getPlato: function(){
       var row = this.getSelectionModel().getSelection();
       return row[0].get('plato');
   },
   successHandler: function(response){
        var re = Ext.JSON.decode(response.responseText);
        if(re.success == true){
           var grid = Ext.create('KMA.view.grids.AlumnosPlatoArmado',{
               count: re.countAlumnos
           });
           grid.getStore().load();
           Ext.create('KMA.view.wins.AlumnosPlatoArmado',{
               itemToAdd: grid,
               titulo: re.gridTitle,
           }).show();
        }else{
            Ext.create('KMA.view.environment.Error',{
                 titulo: "Ver alumnos del plato",
                 txt: re.msg
             }).showDialog();
        }
   },
   generarReporteEnPapel: function(aForm){
        Ext.Ajax.request({
            method: 'POST',
            url: 'src/Drivers/PlatosArmados/generarReporte.php',
            params: {
                fecha: aForm.getFecha(),
                nivel: aForm.getNivel(),
                tiposubmenu: aForm.getTipoSubmenu(),
                platos: aForm.getPlatos()
            },
            success: function(response){
                var re = Ext.JSON.decode(response.responseText);
                if(re.success){
                    Ext.create('KMA.view.environment.Ok',{
                        titulo: 'Reportes',
                        txt: re.msg
                    }).showDialog();
                }else{
                    Ext.create('KMA.view.environment.Error',{
                        titulo: 'Reportes',
                        txt: re.msg
                    }).showDialog();
                }
            }
        });
        
    }
   
});